<!--
  作者：黄俊文，
  时间：2019-05-11，
  版本：1.0，
  使用：<zc-header :id="1"></zc-header>
  参数：id,
  描述：项目固定页头，包括页面导航，参数 id 标注当前导航选项下标，出示下标为：1
-->

<template>
	<div :class="'ywHome'">
		<nav :class="['navbar', 'navbar-expand-md', 'navbar-dark container']" :style="{'background-color':'white','background-image':'linear-gradient(to bottom,#fff,#fff)','border-color':'#f5f5f5','min-width':'1200px!important'}">
			<RouterLink to="/yw_Home" :class="['navbar-brand']">
				<img :src="zcDomain +'static/RSImg/logo.png'" />
			</RouterLink>
			<a :class="['navbar-brand']"><img :src="zcDomain +'static/RSImg/logos.png'" @click="windowOpen" style="cursor: pointer"/></a>
			<div :class="['collapse', 'navbar-collapse']">
				<ul :class="['navbar-nav','mr-auto']" :style="{visibility:isNav?'visible':'hidden'}">
					<li style="cursor: pointer" v-for="(item, index) in navLists" :key="index" :class="['nav-item',item.isActive?'active':'']" @click="doNav(item)">
						<a  v-text="item.name" ></a>
					</li>
				</ul>
				<ul :class="['navbar-nav']">
					<li :class="'nav-item'">
						<a  v-text="login.Name" @click="doGoLogin"></a>
					</li>
					<li :class="'nav-item'"><strong>|</strong></li>
					<li :class="'nav-item'"  style="cursor: pointer">
						<a @click="doRegister()" v-text="register.Name" v-show="!isGoHome"></a>
            <a @click="doisGoHome(navLists[0].href)"  v-text="navLists[0].name" v-show="isGoHome"></a>
					</li>
          <!--<li :class="'nav-item'" v-show="isGoHome">-->
            <!---->
          <!--</li>-->
				</ul>
			</div>
		</nav>
	</div>
</template>


<script>
	export default({
		name:'zcHeader',
		inject:['reload'],
		data() {
			return {
				zcDomain:this.$ZcPublic.Domain,
				register:{
					Name:'注册',
					url:'/bsRegist'
				},
				login:{
					Name:'登录',
					url:'/bsLogin'
				},
				navLists:[
					{name:'首页',isActive:false,href:'/yw_Home',Id:1},
					{name:'活动',isActive:false,href:'/bsActivePage',Id:2},
					{name:'资源',isActive:false,href:this.$ZcPublic.Server.ServerUrl+'VueResourceNewManagement/ResourcePage_RS.html',Id:3},
					{name:'收藏',isActive:false,href:this.$ZcPublic.Server.ServerUrl+'VueResourceNewManagement/Collection_RS.html',Id:4},
					{name:'历史',isActive:false,href:this.$ZcPublic.Server.ServerUrl+'VueResourceNewManagement/History_RS.html',Id:5},
					{name:'管理入口',isActive:false,href:this.$ZcPublic.Server.ServerUrl+'VueResourceNewManagement/Management_RS.html',Id:6},
					{name:'证书查询',isActive:false,href:this.$ZcPublic.Server.ServerUrl+'certificate.html?organCode=01',Id:7}
				]
			}
		},
		props:{
			id:Number,
			isNav:{
				type: Boolean,
				default:true
			},
      isGoHome:{
			  type:Boolean,
        default:false
      }
		},
		methods: {
      windowOpen(){
        window.open("http://www.80paper.com/edu/scjky")
      },
			init(){
				let userId= this.$ZcPublic.UserId;

				console.log('userId',userId)
				if (userId && userId != 'null' && sessionStorage.getItem('userName')) {
					this.register.Name = '退出';
					this.register.url = ''
					this.login.Name = sessionStorage.getItem('userName');
					this.login.url = ''

				} else {
					this.login.Name = '登录';
					this.login.url = '/bsLogin'
				}

			},
      doGoLogin(){
        localStorage.setItem("JumpKey", window.location.href);
		this.$router.push(this.login.url)
      },
      do_loginFree(){
let me=this;
        me.$ZcPublic.Server.Post({
          IsNeedLogin:'false',
          Paras:{
            Name: 'Vue_CustomerResource',
            Action: 'Ext_ResourceManagement_Login',
            Data:{
              Telephone: me.user.Telephone,
              Password: me.user.password
            }
          },
          Callback:me.cb_do_login,
          Scope:me
        });
      },
      doisGoHome(href){
			  this.$router.push(href)
      },
			doNav(item){ //处理导航条切换状态
				let _this = this;

				if(item.name=="活动"){
          sessionStorage.setItem("projectType",'')
        }
        if (item.Id < 3) {
          for(let list in _this.navLists){
            _this.navLists[list].isActive = false;
          }
          item.isActive = true;
        	this.$router.push({path:item.href})
        }
       else if(item.Id==7){

          window.open(this.$ZcPublic.Server.ServerUrl + "certificate.html?organCode=01" )

        }
        else{
          for(let list in _this.navLists){
            _this.navLists[list].isActive = false;
          }
          item.isActive = true;
        	window.location.href = item.href;
        }
			},
			doRegister(){ //注册点击事件
				try{
					console.log('url -',this.register.url)
					if (this.register.url) {
            localStorage.setItem("JumpKey", window.location.href);
						this.$router.push({name:'bsRegist'})

					} else {
						this.$ZcPublic.UserId = null;
            sessionStorage.removeItem('UserId');
						//sessionStorage.setItem('UserId',null);
						this.login.Name = '登录'
						this.login.url = '/bsLogin'

						this.$ZcPublic.Server.Post({
							IsNeedLogin:'true',
							Paras:{
								Name: 'Vue_CustomerResource',
								Action: 'Ext_UserEnd_RS',
								Data:{}
							},
							Callback:this.cb_UserEnd_RS,
							Scope:this
						})
          this.$router.push('/yw_Home')
					}
				}catch(e){
					//TODO handle the exception
					//alert(e)
				}
			},
			cb_UserEnd_RS(resJson,scope) {
				try{
					console.log('退出登录',resJson);

					this.reload();
				}catch(e){
					//TODO handle the exception
					//alert(e)
				}
			},
      /*================回调================*/
      cb_do_login(resJson,scope){
        console.log('自动登录返回')
        console.log(resJson)
        try {
          if(resJson.success==false){
            //this.mistake=true;
            //this.mistakeMsg=resJson.msg
          }else {

            sessionStorage.setItem('UserId',resJson.data.PB_Operator.SN);
            let name=resJson.data.PB_Operator.Name.split('(')[0];
            sessionStorage.setItem("userName",name);
            sessionStorage.setItem("workUnitN",resJson.data.WorkUnitName);
            sessionStorage.setItem("WorkUnitSN",resJson.data.WorkUnitSN);
            this.init();
          }
        }catch (e) {

        }
      },
		},
		mounted() {
			console.log('id =',this.$props.id)
			// console.log(this.navLists)
			for (var i = 0; i < this.navLists.length; i++) {
				if (this.navLists[i].Id === this.$props.id ) {
					console.log(this.navLists[i].name)
					this.navLists[i].isActive = true;
				}
			}
		},
		created () {
			console.log('UserId == ',sessionStorage.getItem('UserId'))
			this.$ZcPublic.UserId = sessionStorage.getItem('UserId');
			this.user=JSON.parse(localStorage.getItem("loginFree"))
			//console.log(this.user)
			if(this.user){
				this.do_loginFree()
			}
			this.init();
		}
	})
</script>

<style scoped>
	 html, body, div, span, applet, object, iframe, a, h1, h2, h3, h4, h5, h6, p, dd, dl, dt, li, ol, ul,
        input, select, button, textarea {
            font-size: 14px!important;
        }
	.nav-item > a {
            color: #333;
            background-color: #fff !important;
            /*text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25) !important;*/
            font-weight:600;
            padding: 10px 10px !important;
            /*margin: 0 12px;*/
            text-decoration: none;

            /*border-bottom: 2px solid #41bafe;*/
        }
        .nav-item > a:hover {
            color: #41bafe !important;
        }
        .inline > li > a {
            float: none;
            padding: 10px 0px;
            text-decoration: none;
        }
        .nav-item > strong {
            display: block;
            /*padding: 10px 0px;*/
            line-height: 1.4;
            color: #999;
            /*text-shadow: 0 -1px 0 rgba(0,0,0,0.25);*/
            font-weight: 600;
        }
		.active > a {
            color: #41bafe !important;
            /*background-color:#fff!important;
            box-shadow: inset 0 3px 8px rgba(0, 0, 0, 0)!important;
            padding:10px 0!important;
            margin:0 15px;*/
            border-bottom: 2px solid #41bafe;
        }
		.ywHome{
			min-width: 1200px!important;
			max-width: 100%;
			background-color: #fff;
		}
</style>
